home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Linux Cubed Series 7: Sunsite
/
Linux Cubed Series 7 - Sunsite Vol 1.iso
/
system
/
admin
/
linuxcon.000
/
linuxcon
/
linuxconf-1.6
/
dialog
/
menubox.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-08-01
|
8KB
|
364 lines
#include "diadef.h"
#include "dialog.h"
#include "dialog.m"
class FIELD_MENU: public FIELD_STRING{
int column2;
char *tag;
/*~PROTOBEG~ FIELD_MENU */
public:
FIELD_MENU (const char *_tag, const char *_str);
protected:
void dokey (WINDOW *, int , FIELD_MSG&);
public:
void drawgen (WINDOW *win, int selected);
void drawtxt (WINDOW *dialog);
void format_htmlkey (char *key, int);
const char *getmenustr (void);
protected:
int getwidth1 (void);
public:
void html_draw (int nof);
int html_validate (int);
protected:
void restore (void);
void save (void);
public:
void setcursor (WINDOW *dialog);
void setwidth1 (int _column2);
void unselect (WINDOW *dialog);
~FIELD_MENU (void);
/*~PROTOEND~ FIELD_MENU */
};
PUBLIC FIELD_MENU::FIELD_MENU(
const char *_tag,
const char *_str)
: FIELD_STRING ("",(char*)_str,60)
{
tag = strdup(_tag);
setwidth1 (strlen(_tag)); // Fixed to some value until
// setwidth1() is called, much later
}
PUBLIC FIELD_MENU::~FIELD_MENU()
{
free (tag);
}
/*
Build a key that uniquely identify this field in the dialog
*/
PUBLIC void FIELD_MENU::format_htmlkey(char *key, int)
{
html_formatkey (key,"%s %s",tag,buf);
}
/*
Fix the final disposition of the menu item
*/
PUBLIC void FIELD_MENU::setwidth1 (int _column2)
{
column2 = _column2;
box.width = _column2 + 2 + strlen(buf);
}
/*
Get the width of the first column.
*/
PROTECTED int FIELD_MENU::getwidth1 ()
{
return column2;
}
/*
Return the second string of a menu item
*/
PUBLIC const char *FIELD_MENU::getmenustr()
{
return str;
}
/*
A FIELD_MENU is not really a string editor.
The following function are just there to prevent writing
over constant string.
*/
PROTECTED void FIELD_MENU::save()
{
}
PROTECTED void FIELD_MENU::restore()
{
}
PROTECTED void FIELD_MENU::dokey(
WINDOW *,
int,
FIELD_MSG & )
{
}
/*
Print menu item
*/
PUBLIC void FIELD_MENU::drawgen(WINDOW *win, int selected)
{
wattrset(win, menubox_attr);
wmove(win, box.y, box.x);
int i;
// Cleanup
for (i = 0; i < box.width; i++) waddch(win, ' ');
wmove(win, box.y, box.x);
char *pttag = tag;
if (tag[0] != ' '){
wattrset(win, selected ? tag_key_selected_attr : tag_key_attr);
waddch(win, tag[0]);
pttag++;
}
wattrset(win, selected ? tag_selected_attr : tag_attr);
waddstr(win, pttag);
wmove(win, box.y, box.x + column2 + 2);
wattrset(win, selected ? item_selected_attr : item_attr);
waddstr(win, buf);
wmove(win, box.y, box.x);
}
PUBLIC void FIELD_MENU::unselect(WINDOW *dialog)
{
drawgen(dialog,0);
}
PUBLIC void FIELD_MENU::setcursor(WINDOW *dialog)
{
drawgen (dialog,1);
}
PUBLIC void FIELD_MENU::drawtxt(WINDOW *dialog)
{
drawgen (dialog,0);
}
PUBLIC void FIELD_MENU::html_draw(int nof)
{
html_printf ("<tr><td>%s<td>",tag);
char key[100];
format_htmlkey (key,nof);
html_setaref (key,buf);
html_printf ("\n");
}
PUBLIC int FIELD_MENU::html_validate(int)
{
return 0;
}
/*
Evaluate the width of the first column.
Options are organisez in 2 columns.
*/
PRIVATE void DIALOG::fixwidth1()
{
int max_width1=0;
int n = getnb();
int i;
for (i=0; i<n; i++){
FIELD *f = getitem(i);
int w = f->getwidth1();
if (w > max_width1) max_width1 = w;
}
for (i=0; i<n; i++) getitem(i)->setwidth1(max_width1);
}
/*
Add a new menu item to a dialog
You will have to use the DIALOG::editmenu instead of just
DIALOG::edit() or your dialog will lack some buttons and won't
be layout properly.
*/
PUBLIC void DIALOG::new_menuitem (const char *prompt1, const char *prompt2)
{
if (strcmp(prompt1,"-")==0){
/* #Specification: dialog / menus / splitter
If the first member of a menu entry
is a single '-', this entry is
drawn as a full splitter line
The second entry will be used as the
splitter title.
*/
newf_title ("",prompt2);
}else{
if (prompt1[0] == '\0') prompt1 = " ";
FIELD_MENU *men = new FIELD_MENU (prompt1,prompt2);
add (men);
}
}
/*
Add a new menu item to a dialog
You will have to use the DIALOG::editmenu instead of just
DIALOG::edit() or your dialog will lack some buttons and won't
be layout properly.
*/
PUBLIC void DIALOG::new_menuitem (const char *prompt1, const SSTRING &prompt2)
{
new_menuitem (prompt1,prompt2.get());
}
PUBLIC void DIALOG::new_menuitem (const SSTRING &prompt1, const SSTRING &prompt2)
{
new_menuitem (prompt1.get(),prompt2);
}
/*
Return the second string of a menu item
*/
PUBLIC const char *DIALOG::getmenustr(int choice)
{
const char *ret = NULL;
if (choice >=0 && choice < getnb()){
ret = getitem(choice)->getmenustr();
}
return ret;
}
/*
Record a small help for the Save button
*/
PUBLIC void DIALOG::savewhat (const char *help)
{
what.save.setfrom (help);
}
/*
Record a small help for the Del button
*/
PUBLIC void DIALOG::delwhat (const char *help)
{
what.del.setfrom (help);
}
/*
Record a small help for the Ins button
*/
PUBLIC void DIALOG::inswhat (const char *help)
{
what.ins.setfrom (help);
}
/*
Record a small help for the Add button
*/
PUBLIC void DIALOG::addwhat (const char *help)
{
what.add.setfrom (help);
}
static void append_what (
int &options,
int optval,
SSTRING &str,
const char *msg,
SSTRING &explan)
{
if (!str.is_empty()){
char buf[100];
sprintf (buf,MSG_U(I_SELECT
,"\nSelect <%s> to %s %s"),msg,msg,str.get());
options |= optval;
explan.append (buf);
}
}
/*
Edit a menu
*/
PUBLIC MENU_STATUS DIALOG::editmenu(
const char *title,
const char *prompt,
const char *helpfile,
int &sel, // Will hold the selection or -1 if escape
// It must already contains the initial selection
int options) // MENUBUT_ADD | MENUBUT_INS | MENUBUT_DEL | MENUBUT_SAVE
{
SSTRING tmp_explan (prompt);
int spc_options = 0;
append_what (spc_options,MENUBUT_SAVE,what.save
,MSG_U(I_SAVE,"save"),tmp_explan);
append_what (spc_options,MENUBUT_ADD,what.add
,MSG_U(I_ADD,"add"),tmp_explan);
append_what (spc_options,MENUBUT_INS,what.ins
,MSG_U(I_INS,"ins"),tmp_explan);
append_what (spc_options,MENUBUT_DEL,what.del
,MSG_U(I_DEL,"del"),tmp_explan);
fixwidth1();
return edit (title,tmp_explan.get(),helpfile,sel
,options|spc_options|MENUBUT_OK|MENUBUT_QUIT);
}
PUBLIC MENU_STATUS DIALOG::editmenu(
const char *title,
const char *prompt,
HELP_FILE &helpfile,
int &sel, // Will hold the selection or -1 if escape
// It must already contains the initial selection
int options) // MENUBUT_ADD | MENUBUT_INS | MENUBUT_DEL | MENUBUT_SAVE
{
return editmenu (title,prompt,helpfile.getpath(),sel,options);
}
/*
* Display a menu for choosing among a number of options
* Return MENU_OK, MENU_QUIT or MENU_ESCAPE
* Depending on the options parameter, may return MENU_INS, MENU_SAVE
* MENU_ADD or MENU_DEL.
*/
MENU_STATUS dialog_menu(
const char *title,
const char *prompt,
const char *helpfile,
int options, // MENUBUT_ADD | MENUBUT_INS | MENUBUT_DEL | MENUBUT_SAVE
int item_no,
char **items,
int &sel) // Will hold the selection or -1 if escape
// It must already contains the initial selection
{
DIALOG dia;
for (int i=0; i<item_no; i++){
dia.new_menuitem(items[i*2],items[i*2+1]);
}
return dia.editmenu (title,prompt,helpfile,sel,options);
}
#ifdef TEST
int main (int argc, char *argv[])
{
int ret,choice=0;
static char *tb[]={
"a", "message",
"allo", "alal",
"b", "bbbbbb",
"bozo", "bzbzbzbz",
"c", "coco",
"coco", "cocococococ",
"d", "dddd",
"dozo", "dozozododo",
};
init_dialog();
if (argc == 2){
dialog_settimeout(atoi(argv[1]), '\n');
}
ret = dialog_menu(
"This is a test"
,"Are you\nsure you want to exit"
,"/tmp/dialog.bak"
,MENUBUT_SAVE
,8,tb,choice);
ret = dialog_menu(
"This is a test"
,"Are you\nsure you want to exit\n"
,"/tmp/dialog.bak"
,MENUBUT_SAVE | MENUBUT_ADD | MENUBUT_DEL
,8,tb,choice);
endwin();
printf ("ret = %d choice = %d\n",ret,choice);
return 0;
}
#endif